const Vue = require("../common/vue");
let order_service = require("../../service/order-service");
let evaluation_service = require("../../service/evaluation_service");
let _util = require('util/util.js');
require('../common/header/index')
require('../common/sidebar/index')

let order_detail = new Vue({
    el: "#order-detail",
    data() {
        return {
            order: "",
            reason: "",
            total: 0,
            commentitemid: 0,
            commentitemname: "",
            commentText: "",
            refusalReason: ""
        }
    },
    methods: {
        getOrderItems: function(_this){
            order_service.getOrderDetail(_this.orderID, function(res) {
                _this.order = res;
            }, function(err) {
                console.log(err);
            });
        },
        getTotal: function(_this) {
            let res = 0.0;
            console.log(_this.order.orderItemVOList);
            for(let orderitem of JSON.parse(JSON.stringify(_this.order.orderItemVOList))){
                res += orderitem.allprice;
            }
            _this.total = res;
        },
        refactor: function(src) {``
            let n = src.length;
            let newSrc = "/image" + src.substr(18, n - 20);
            console.log(newSrc);
            return newSrc;
        },
        pay: function() {
            let _this = this;
            order_service.postPay(this.orderID, function(res){
                window.location.href = "/view/order_detail.html?orderID=" + _this.orderID;
            }, function(err) {
                console.log(err);
            })
        },
        alipay: function() {
            order_service.postAlipay(this.orderID, this.total, function(resdata){
                console.log(resdata);
                const div=document.createElement('div-alipay');
                div.innerHTML=resdata;
                console.log(div);
                document.body.appendChild(div);
                document.forms[4].submit();
            }, function(err) {
                console.log(err);
            })
        },
        deleteOrder: function() {
            order_service.deleteOrder(this.orderID, function(){
                window.location.href = "/view/orders_all.html?status=-1";
            }, function(err) {
                console.log(err);
            });
        },
        returnOrder: function() {
            order_service.returnOrder(
                this.orderID, 
                {
                    "refundreason": this.reason, 
                },
                function() {
                window.location.href = "/view/orders_all.html?status=-1";
            }, function(err) { 
                console.log(err);
            })
        },
        openComment: function(itemid, name) {
            this.commentitemid = itemid;
            $("#comment-form").modal();
            $("#comment-item-name").text("对商品 "+name+" 的评价");
        },
        submitComment: function() {
            let _this = this;
            evaluation_service.postComment(this.commentitemid, this.commentText, 5.0, function(res) {
                window.location.href = "/view/product_details.html?itemid=" + _this.commentitemid;
            }, function(err) {
                console.log(err);
            })
        },
        getRefundInfo: function (self) {
            order_service.getRefundInfo(
                self.orderID,
                function (res) {
                    self.refusalReason = res.refusalreason;
                },
                function (errMsg) {
                    console.log(errMsg)
                }
            )
        },
    },
    created: function(){
        this.orderID = _util.getQueryVariable("orderID");
        this.getOrderItems(this);
        this.getTotal(this);
        this.getRefundInfo(this);
    }
});

